<template>
  <el-dialog v-model="dialogFormVisible" title='审核产品' width="30%" center>
    <div class="describe">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;确定审核此产品的出库吗？</div>
    <el-radio-group v-model="formData.product_out_status" class="ml-4">
			<!-- 单选框 -->
			<el-radio label="同意" size="large" value="同意">同意</el-radio>
			<el-radio label="否决" size="large" value="否决">否决</el-radio>
		</el-radio-group>
		<el-input type="textarea" placeholder="请输入审核备注" v-model="formData.audit_memo" />
		<template #footer>
			<span class="dialog-footer">
				<el-button type="primary" @click="productAudit">确定</el-button>
			</span>
		</template>
  </el-dialog>
</template>
 
<script setup lang="ts">
  import { reactive, ref, onBeforeUnmount } from 'vue';
  import { ElMessage } from 'element-plus';
  import { bus } from "@/utils/mitt.js";
  import { useUserInfo } from '@/store/userinfor.js';
  import { auditProduct } from '@/api/product.js';
  import { tracking } from '@/utils/operation.js';
  
  const userStore = useUserInfo()

  bus.on('applyProductInfo', (row : any) => {
		formData.id = row.id
		formData.product_out_id = row.product_out_id
		formData.product_name = row.product_name
		formData.product_out_status = row.product_out_status
		formData.audit_memo = row.audit_memo
		formData.product_out_price = row.product_out_price
		formData.product_out_apply_person = row.product_out_apply_person
		formData.product_in_warehouse_number = row.product_in_warehouse_number
		formData.product_single_price = row.product_single_price
		formData.product_out_number = row.product_out_number
		formData.product_apply_time = row.product_apply_time
	})

	const formData = reactive({
		id: 0,
		product_name:'',
		product_out_id: 0,
		product_out_status: '',
		audit_memo: '',
		product_out_price: 0,
		product_out_audit_person: userStore.name,  // userStore
		product_out_apply_person: '',
    product_in_warehouse_number: 0,
		product_single_price: 0,
		product_out_number: '',
		product_apply_time:''
	})

  const emit = defineEmits(['success'])

  const productAudit = async () => {
    const res = await auditProduct(formData) 
		if (res.data.status == 0) {
			ElMessage({
				message: '审核产品完成',
				type: 'success',
			})
			emit('success')

			// module, operation_person, operation_objcet, operation_level, operation_status
			tracking('产品', localStorage.getItem('name'), formData.product_name, '高级', formData.product_out_status)
			dialogFormVisible.value = false
		}else{
			ElMessage.error('审核产品失败，出库编号重复')
			dialogFormVisible.value = false
		}
  }

  // ------------------------------------------------------------------------------弹窗开关
	const dialogFormVisible = ref(false)
  // 打开创建管理员的弹窗
  const open = () => {
    dialogFormVisible.value = true
  }
  defineExpose({
    open
  })
  onBeforeUnmount(() => {
		bus.all.clear()
	})
</script>
 
<style lang="scss" scoped>
	.describe {
		margin-bottom: 8px;
	}
</style>
